package 剑指offer;

import leetcode.ListNode;
import leetcode.day_2020_12_14_12_21.TreeNode;

public class Offer_JZ26 {
    static TreeNode pre, head;
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null) return null;
        dfs(pRootOfTree);
        head.left = pre;
        pre.right = head;
        return head;
    }

    public static void dfs(TreeNode root) {
        if (root == null) return;
        dfs(root.left);
        if (pre == null) { // 说明访问的是头结点
            head = root;
        } else {
            pre.right = root;
        }
        root.left = pre;
        pre = root;
        dfs(root.right);
    }
}
